<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'buyseed';
$return=array();
$return['setting']=$this->plugin_setting('basic');
$data=array('1','3','5','10','20');
if($_RQ['op']=='buyseed'){
	//购买种子
	if(!$_RQ['id']){
		exi('没有找到该种子，请重新选择！','error','jy_farmer/seed/list');
	}
	$seed=pdo_get('jy_farmer_seed',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	if($seed['down_send_type0']!=1 && $return['setting']['down_send_type0']!=1){
		$return['sendtypes'][0]='店内自提';
		$sendtype=0;
	}
	if($seed['down_send_type1']!=1 && $return['setting']['down_send_type1']!=1){
		$return['sendtypes'][1]='快递配送';
		$sendtype=1;
	}
	$this->session('seed_id',$_RQ['id']);
	$num=array();
	foreach($data as $item){
		if($item<=$seed['num']){
			array_push($num,$item);
		}
	}
	$return['seed']=$seed;
	$return['num']=$num;
	$return['data']=$data;
	$return['selectstore']=$return['setting']['stores'][0]?:array();
	$return['post']=array('sendtype'=>$sendtype,'total'=>1,'pcd'=>array('city'=>'深圳市'),'address'=>array('pcd'=>array('city'=>'深圳市')));
	$address=pdo_get('core_address',array('isdel'=>0,'isdefault'=>1,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	if(!$address){
		$address=pdo_get('core_address',array('isdel'=>0,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	}
	$address['pcd']=json_decode($address['pcd'],true);
	$return['post']['address']=$address;
	$return['post']['addressid']=$address['id'];
	$order=pdo_get('jy_farmer_order',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'username <>'=>''),array('username','mobile'),'id desc');
	$return['post']['username']=$order['username'];
	$return['post']['mobile']=$order['mobile'];
	exi($return);
}

if($_RQ['op']=='notice'){
	$post=get_postdata();
	$seed_id = $this->session('seed_id');
	if($post){
		if($post['sendtype']!=1 && empty($post['username'])){
				exi('请输入姓名','error');
			}
			if($post['sendtype']!=1 && empty($post['mobile'])){
				exi('请输入联系电话','error');
			}
			if($post['sendtype']!=1 && !preg_match("/^1[345789]\d{9}$/ims",$post['mobile'])){  	 
				exi('请输入正确的手机号码','error');
			}
			if($post['sendtype']==1 && (!$post['address']['pcd']['province'] || !$post['address'])){
				exi('请填收货地址！','error');
			}
			$seed=pdo_get('jy_farmer_seed',array('uniacid'=>$_SESSION['uniacid'],'id'=>$seed_id));
			$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'seed_id'=>$this->session('seed_id'),
				'username'=>$post['username']?:$post['address']['name'],
				'mobile'=>$post['mobile']?:$post['address']['mobile'],
				'address'=>$post['pcd']['province'].' '.$post['pcd']['city'].' '.$post['pcd']['district'].' '.$post['pcd']['street'].' '.$post['address'],
				'num'=>$data[$post['numid']],
				'money'=>$seed['price']*$data[$post['numid']],
				'ordersn'=>get_ordersn('jy_farmer_order','ordersn'),
				'createtime'=>TIMESTAMP,
			);
			pdo_insert('jy_farmer_order',$insert);
			$id=pdo_insertid();
			$params['fee']=$seed['price']*$data[$post['numid']];
			$params['title']='购买'.$seed['title'];
			$params['id']=$id;
			$params['plugin']='jy_farmer';
			$params['back_url']='jy_farmer/buyseed/payresult';
			$payid=cfc('pay')->create_pay_order($params);
			$this->pdo_update('order',array('pay_id'=>$payid),array('id'=>$id));
			$res=array(
				'pay_id'=>$payid,
				'order_id'=>$id,
			);
			exi($res);
	}
	
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error','list');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error','list');
	}
	$order=pdo_get('jy_farmer_order',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	if($order['money']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	pdo_update('jy_farmer_order',array('status'=>1,'pay'=>$payresult['fee']),array('id'=>$order['id']));
	$seed=pdo_get('jy_farmer_myseed',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'seed_id'=>$order['seed_id']));
	if($seed){
		$update=array(
			'sum'=>$seed['sum']+$order['num'],
			'createtime'=>TIMESTAMP,
		);
		pdo_update('jy_farmer_myseed',$update,array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'seed_id'=>$order['seed_id']));
	}else{
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'seed_id'=>$order['seed_id'],
			'sum'=>$order['num'],
			'order_id'=>$order['id'],
			'createtime'=>TIMESTAMP,	
		);
		pdo_insert('jy_farmer_myseed',$insert);
	}
	pdo_update('jy_farmer_seed',array('num -='=>$order['num']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['seed_id']));
	$this->model('order')->pay($order);
	exi('支付成功','success','jy_farmer/myseed/seed/');
}

if($_RQ['op']=='money'){
	if($_RQ['id']===''){
		exi('请选择种子','error');
	}
	$seed=pdo_get('jy_farmer_seed',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$money=$data[$_RQ['num']]*$seed['price'];
	$return['money']=$money;
	$return['success']=1;
	exi($return);
}